package com.mairuide.robot.IKAnalyzer;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.dic.Dictionary;

import com.mairuide._frame.utils.StringUtils;
import com.mairuide.robot.participles.MParticiplesService;

/**
 * 自动加载新词库
 */
@Component
@EnableScheduling   // 2.开启定时任务
public class WordSyncJob {
	
    @Autowired 
    private MParticiplesService mParticiplesService;
    /**
     * 最后一次加载关键词的时间
     */
    private LocalDateTime lastUpdatedTime = LocalDateTime.of(1970,1,1,0,0,0);
    
    @Scheduled(cron = "0/10 * * * * ?")
    public void loadWord(){
        lastUpdatedTime = lastUpdatedTime.minusSeconds(10);
        List<String> wordList = new ArrayList<String>();
        wordList = mParticiplesService.getParticiples4CreateTime(StringUtils.LocalDateTimeToUdate(lastUpdatedTime));
        Configuration cfg = DefaultConfig.getInstance();
        Dictionary.initial(cfg);
        Dictionary dic = Dictionary.getSingleton();
        dic.addWords(wordList);
        lastUpdatedTime = LocalDateTime.now();
    }
}
